﻿import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:vischat/core/annotations/getx_route_enhanced.dart';
import 'package:vischat/core/constants/asset_paths.dart';
import 'package:vischat/generated/icons/app_icons.dart';
import 'package:vischat/modules/home/user/edit_user/edit_user_binding.dart';
import 'package:vischat/modules/home/user/edit_user/edit_user_controller.dart';
import 'package:vischat/shared/widgets/avatar.dart';

@GetXRoute('/user/edit', binding: EditUserBinding)
class EditUserPage extends GetView<EditUserController> {
  const EditUserPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SizedBox(
        height: double.infinity,
        child: Stack(
          children: [
            Container(
              height: 400.h,
              padding: EdgeInsets.all(60.w),
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(AssetPaths.defaultUserCover),
                  fit: BoxFit.fill,
                ),
              ),
              child: Container(
                margin: EdgeInsets.only(top: 30.h),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    GestureDetector(
                      onTap: () => Get.back(),
                      child: Container(
                        padding: EdgeInsets.all(10.w),
                        decoration: BoxDecoration(
                          color: Colors.black.withAlpha(50),
                          borderRadius: BorderRadius.circular(50.r),
                        ),
                        child: Icon(
                          AppIcons.back,
                          color: Colors.white,
                          size: 40.r,
                        ),
                      ),
                    ),
                    GestureDetector(
                      onTap: controller.selectCover,
                      child: Container(
                        padding: EdgeInsets.symmetric(
                          horizontal: 20.w,
                          vertical: 10.h,
                        ),
                        decoration: BoxDecoration(
                          color: Colors.black.withAlpha(50),
                          borderRadius: BorderRadius.circular(20.r),
                        ),
                        child: Row(
                          children: [
                            Icon(
                              AppIcons.camera_add,
                              color: Colors.white,
                              size: 40.r,
                            ),
                            SizedBox(width: 6.w),
                            Text(
                              'button.cover'.tr,
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 24.sp,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Positioned(
              top: 380.h,
              bottom: 0,
              left: 0,
              right: 0,
              child: Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(12.r),
                    topRight: Radius.circular(12.r),
                  ),
                ),
                child: _userInfoEdit(),
              ),
            ),
            Positioned(
              top: 320.h,
              left: 0,
              right: 0,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Stack(
                    children: [
                      Obx(
                        () => AppAvatar(
                          avatar: controller.userInfo.avatar ?? '',
                          onTap: controller.selectAvatar,
                          size1: 80.r,
                          size2: 75.r,
                        ),
                      ),
                      const Positioned(
                        right: 0,
                        bottom: 0,
                        child: Icon(AppIcons.camera, color: Colors.blue),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _userInfoEdit() {
    return Container(
      margin: EdgeInsets.only(top: 100.h),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              SizedBox(width: 24.w),
              Text(
                controller.userInfo.name ?? '',
                style: TextStyle(fontSize: 36.sp, fontWeight: FontWeight.bold),
              ),
              SizedBox(width: 10.w),
              GestureDetector(
                onTap: controller.switchEditState,
                child: Icon(
                  AppIcons.edit,
                  color: Colors.lightBlueAccent,
                  size: 48.r,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
